<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use GeetestLib;
use Illuminate\Support\Facades\DB;
use App\Models\AdminUser;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    /**
     * 登录页面
     *
     * @return mixed
     */
    public function index()
    {
        return view('admin/login/index');
    }

    /**
     * 退出登录
     *
     * @return mixed
     */
    public function logout()
    {
        Auth::logout();
        session()->forget('adminInfo');
        return redirect('/admin/login/index');
    }
    public function login(Request  $request)
    {
        // dd(session("geetest"));
        $GtSdk = new GeetestLib(config('sys.GEE_ID'), config('sys.GEE_KEY'));
        $gtserver = session("geetest");
        $geetest_challenge = $request->input('geetest_challenge');
        $geetest_validate = $request->input('geetest_validate');
        $geetest_seccode = $request->input('geetest_seccode');
        $data = array(
              "client_type" => "web", #web:电脑上的浏览器；h5:手机上的浏览器，包括移动应用内完全内置的web_view；native：通过原生SDK植入APP应用的方式
              "ip_address" => $request->ip() # 请在此处传输用户请求验证时所携带的IP
            );
        $message = '验证失败,请稍后重试!';
        if ($gtserver == 1) {
            $result = $GtSdk->success_validate($geetest_challenge, $geetest_validate, $geetest_seccode, $data);
            if ($result) {
                $code_flag = true;
            } else{
                $code_flag = false;
            }
        } else {
            if ($GtSdk->fail_validate($geetest_challenge, $geetest_validate, $geetest_seccode, $data)) {
                 // 验证码验证成功
                $code_flag = true;
            }
        }
        if ($code_flag) {
            //编写验证逻辑
            //encrypt($request->secret); decrypt($encryptedValue);
            $name = $request->name;
            $password = $request->pass;
            
            // $list = compact('name','password','last_ip','create_at');
            
            // $uid = $adminM->checkAdminUser($params);
            // dd(Auth::attempt(['name' => $name, 'password' => $password]));Auth::check()
            if (Auth::attempt(['name' => $name, 'password' => $password,'status'=>1])) {
                $user = Auth::user()->toArray();
                session(['adminInfo'=>$user]);
                // dd($user);
                $uid = $user['id'];
                $change['last_ip'] = $request->ip();
                $change['last_at'] = time();
                $adminM = new AdminUser();
                $adminM->updateAdminUser($uid,$change);
                return redirect('/admin/index/index');
            } else {
                return redirect()->back()->with('message', '账号或密码错误/账号已禁用');
            }
        } else {
            return redirect()->back()->with('message', $message);
        }
    }
    public function getVerify(Request  $request){
        //实例化并传入极验id与key值
        $GtSdk = new GeetestLib(config('sys.GEE_ID'), config('sys.GEE_KEY'));
        $data = array(
//            "user_id" => "test", # 网站用户id
            "client_type" => "web", #web:电脑上的浏览器；h5:手机上的浏览器，包括移动应用内完全内置的web_view；native：通过原生SDK植入APP应用的方式
            "ip_address" => $request->ip() # 请在此处传输用户请求验证时所携带的IP
        );
        $status = $GtSdk->pre_process($data, 1);
        session(['geetest'=>$status]);
        echo $GtSdk->get_response_str();
    }
}
